package com.bifrost.wechat.controller;

import com.bifrost.common.model.ResultModel;
import com.bifrost.entity.MerchantStore;
import com.bifrost.repository.MerchantStoreRepository;
import com.bifrost.wechat.entity.WechatAccount;
import com.bifrost.wechat.service.WechatAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 *  @Author:Sophia
 *  @Date:2018/4/9
 *  
 */
@RestController
@Api("后台门店微信支付接口")
@RequestMapping("/admin/pay/")
public class WechatAccountController {

    @Autowired
    WechatAccountService wechatAccountService;
    @Autowired
    MerchantStoreRepository merchantStoreRepository;

    @ApiOperation("门店微信支付信息")
    @PostMapping("/add")
    public ResultModel addAccout(@RequestBody @Valid WechatAccount wechatAccount){
        MerchantStore store = merchantStoreRepository.findOne(wechatAccount.getStoreId());
        wechatAccount.setStoreCode(store.getStoreCode());
        WechatAccount account=wechatAccountService.save(wechatAccount);
        if (account != null) {
            return ResultModel.ok("成功");
        }
        return ResultModel.error("新增信息失败");
    }

    @ApiOperation("获取支付信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "storeId",value = "门店ID",dataType = "Long",required = true)
    })
    @GetMapping("/detail")
    public ResultModel getAccount(@RequestParam Long storeId){
        WechatAccount wechatAccount=wechatAccountService.getAccount(storeId);
        if (wechatAccount==null){
            return ResultModel.error("未找到数据");
        }
        return ResultModel.ok("成功").put("wechatAccout",wechatAccount);
    }

    @ApiOperation("更新支付信息")
    @PostMapping("/update")
    public ResultModel updateAccout(@RequestBody WechatAccount wechatAccount){
        MerchantStore store = merchantStoreRepository.findOne(wechatAccount.getStoreId());
        wechatAccount.setStoreCode(store.getStoreCode());
        WechatAccount checkAccount=wechatAccountService.getAccountById(wechatAccount.getId());
        if (checkAccount==null){
            return ResultModel.error("未找到数据");
        }
        WechatAccount account=wechatAccountService.save(wechatAccount);
        if (account==null){
            return ResultModel.error("更新数据失败");
        }
        return ResultModel.ok("成功");
    }

}
